Everything about Identity Column totally explained
An Identity column is a column (also known as a field ) in a databasetable that (1) uniquely identifies every row in the table, and (2) is made up of values generated by the database. This is much like an AutoNumber field in Microsoft Access or a sequence in Oracle. Because the concept is so important in databasescience, all RDBMS systems implement some type of generated key, although each has its own terminology.
An identity column differs from a primary key in that its values are managed by the server and (except in rare cases ) can't be modified. In many cases an identity column is used as a primary key, however this isn't always the case.
Two types of identity columns are available in SQL Server: incremental (where the user can set a seed and an increment ) and random (where the server chooses a random numeric value and ensures that it hasn't already been used ).
It is often useful or necessary to know what identity value was generated by an INSERT command. SQL Server provides several functions to do this: @@Identity provides the last value generated on the current connection, while Scope_Identity(tablename) provides the last value generated, regardless of the connection it was created on.
Example:
Insert Into Contacts (FirstName, LastName ) Values ('Test', 'User' )
--
Select @@Identity
-- OR --
Declare @ID int
Select @ID = @@Identity
Update Contacts Set Phone = 'XXX-YYY-ZZZZ' Where ContactID = @ID
Further Information
Do you know how hard it is to get a link from a large encyclopaedia? Well we're different and will prove it. To get a link from us just add the following HTML to your site on a relevant page:
Then simply click through this link from your web page. Our crawlers will verify your link, extract the title of your web page and instantly add a link back to it. If you like you can remove the words Totally Explained and embed the link in article text.
As long as your link remains in place, we'll keep our link to you right here. Please play fair - our crawlers are watching. Your site must be closely related to this one's topic. Any kind of spamming, dubious practises or removing the link will result in your link from us being dropped and, potentially, your whole site being banned.